<template>
  <div class="module card">
    <div class="module-header">
      <h2><el-icon><DocumentChecked /></el-icon> 我的问卷</h2>
      <div class="tabs-container">
        <el-radio-group
            :model-value="activeTabW"
            size="small"
            @change="handleTabChange"
        >
          <el-radio-button label="published">我发布的问卷</el-radio-button>
          <el-radio-button label="answered">我填写的问卷</el-radio-button>
        </el-radio-group>
      </div>
    </div>
    <div class="module-body">
      <div v-if="filteredQuestionnaires.length > 0" class="questionnaires-grid">
        <div
            class="questionnaire-item"
            v-for="questionnaire in filteredQuestionnaires"
             :key="questionnaire.id"
            @click="handleQuestionnaireClick(questionnaire)"
        >
          <div class="questionnaire-image">
            <el-image :src="questionnaire.image" fit="cover" class="image-content">
              <template #error>
                <div class="image-error">
                  <el-icon><Document /></el-icon>
                </div>
              </template>
            </el-image>
          </div>
          <div class="questionnaire-details">
            <div class="questionnaire-title">{{ questionnaire.title }}</div>
            <div class="questionnaire-info">
              <span class="questionnaire-type">{{ questionnaire.type === 'published' ? '已发布' : '已填写' }}</span>
              <span class="questionnaire-date">时间: {{ questionnaire.date }}</span>
              <span class="questionnaire-status" :class="questionnaire.status">{{ questionnaire.status === 'ongoing' ? '进行中' : '已结束' }}</span>
            </div>
          </div>
        </div>
      </div>

      <div v-else class="empty-state">
        <el-empty :description="activeTabW === 'published' ? '暂无发布的问卷' : '暂无填写的问卷'" />
      </div>

      <!-- 添加更多按钮 -->
      <div class="more-container">
        <el-button
            class="more-button"
            type="primary"
            link
            @click="handleMoreClick"
        >
          更多问卷 <el-icon><ArrowRight /></el-icon>
        </el-button>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { computed } from 'vue';
import { useRouter } from 'vue-router';
import { DocumentChecked, Document, ArrowRight } from '@element-plus/icons-vue';
import type { PropType } from 'vue';

// 定义问卷数据类型
interface Questionnaire {
  id: number | string;
  title: string;
  type: 'published' | 'answered';
  date: string;
  status: 'ongoing' | 'finished';
  image?: string;
}

const props = defineProps({
  questionnaires: {
    type: Array as PropType<Questionnaire[]>,
    required: true,
    default: () => ([])
  },
  activeTabW: {
    type: String as PropType<'published' | 'answered'>,
    default: 'published'
  }
});

const emits = defineEmits(['update:activeTabW']);
const router = useRouter();

// 过滤问卷数据
const filteredQuestionnaires = computed(() => {
  return props.questionnaires.filter(
      questionnaire => questionnaire.type === props.activeTabW
  );
});

// 处理标签切换
const handleTabChange = (value: 'published' | 'answered') => {
  emits('update:activeTabW', value);
};

// 处理问卷点击
const handleQuestionnaireClick = (questionnaire: Questionnaire) => {
  console.log('点击问卷:', questionnaire.title);
};

// 处理更多按钮点击
const handleMoreClick = () => {
  router.push('/teacher/activities?type=survey');
};
</script>

<style scoped>
/* 完全复用ClassModule的样式，仅修改类名前缀 */
.module {
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  min-height: 200px;
  display: flex;
  flex-direction: column;
}

.module:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.module-header {
  position: relative;
  padding: 16px 20px;
  background: #1e62ec;
  color: white;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.module-header h2 {
  font-size: 18px;
  font-weight: 600;
  margin: 0;
  display: flex;
  align-items: center;
  gap: 10px;
}

.tabs-container {
  display: flex;
  align-items: center;
}

/* 选项卡样式 */
:deep(.el-radio-group) {
  margin-left: 15px;
}

:deep(.el-radio-button) {
  --el-radio-button-checked-bg-color: rgba(255, 255, 255, 0.2);
  --el-radio-button-checked-text-color: white;
  --el-radio-button-checked-border-color: transparent;
}

:deep(.el-radio-button__inner) {
  background: rgba(255, 255, 255, 0.1);
  border: none;
  color: rgba(255, 255, 255, 0.8);
  border-radius: 4px;
  padding: 5px 15px;
  font-size: 13px;
  transition: all 0 .3s;
}

:deep(.el-radio-button__inner:hover) {
  color: white;
  background: rgba(255, 255, 255, 0.15);
}

:deep(.el-radio-button:first-child .el-radio-button__inner) {
  border-radius: 4px 0 0 4px;
}

:deep(.el-radio-button:last-child .el-radio-button__inner) {
  border-radius: 0 4px 4px 0;
}

.module-body {
  padding: 20px;
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}

/* 问卷网格布局 */
.questionnaires-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
  margin-bottom: 20px;
}

.questionnaire-item {
  display: flex;
  padding: 0;
  border-radius: 8px;
  transition: all 0.3s;
  cursor: pointer;
  height: 120px;
  box-sizing: border-box;
  border: 1px solid #ebeef5;
  background: #fff;
  position: relative;
  overflow: hidden;
}

.questionnaire-item:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.questionnaire-image {
  width: 120px;
  height: 120px;
  flex-shrink: 0;
  background: #f5f7fa;
}

.image-content {
  width: 100%;
  height: 100%;
}

.image-error {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
   color: #c0c4cc;
}

.questionnaire-details {
  flex-grow: 1;
  padding: 15px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.questionnaire-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.questionnaire-info {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  font-size: 13px;
  color: #8c8c8c;
}

.questionnaire-type {
  background: #f0f7ff;
  color: #1e62ec;
  padding: 2px 8px;
  border-radius: 4px;
  display: inline-block;
  width: fit-content;
}

.questionnaire-date {
  display: inline-block;
}

.questionnaire-status {
  padding: 2px 8px;
  border-radius: 4px;
  display: inline-block;
  width: fit-content;
}

.questionnaire-status.ongoing {
  background: #f6ffed;
  color: #52c41a;
}

.questionnaire-status.finished {
  background: #fff2f0;
  color: #f5222d;
}

/* 空状态样式 */
.empty-state {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 200px;
  color: #a8a8a8;
  font-style: italic;
  margin-bottom: 20px;
}

/* 更多按钮容器 */
.more-container {
  display: flex;
  justify-content: center;
  margin-top: auto;
  padding-top: 20px;
  border-top: 1px solid #ebeef5;
}

.more-button {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 14px;
  color: #1e62ec;
  transition: color 0.3s;
}

.more-button:hover {
  color: #0e4ec9;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .module-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }

  .tabs-container {
    width: 100%;
    justify-content: flex-end;
  }
}

@media (max-width: 992px) {
  .questionnaires-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 768px) {
  .questionnaire-item {
    height: 100px;
  }

  .questionnaire-image {
    width: 100px;
    height: 100px;
  }

  .questionnaire-details {
    padding: 10px;

  }

  .questionnaire-title {
    font-size: 15px;
  }

  .questionnaire-info {
    font-size: 12px;
  }

  :deep(.el-radio-button__inner) {
    padding: 4px 12px;
    font-size: 12px;
  }

  .more-button {
    font-size: 13px;
  }
}
</style>
